//
//  SetTradingPassWordViewController.m
//  NewPro
//
//  Created by RYX on 2019/4/22.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "SetTradingPassWordViewController.h"
#import "SetStore.h"
#import "SetTradingPassWordSuccessViewController.h"
#import "PutForwardRecordStore.h"
#import "NewWalletList.h"
#import "MineStore.h"
#import "UserModel.h"

@interface SetTradingPassWordViewController ()
@property (weak, nonatomic) IBOutlet UILabel *phoneLa;
@property (weak, nonatomic) IBOutlet UITextField *codeLa;
@property (weak, nonatomic) IBOutlet UITextField *passLa;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@end

@implementation SetTradingPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBarWithTitle:@"设置交易密码"];
    _codeLa.keyboardType = UIKeyboardTypeNumberPad;
    _codeLa.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passLa.keyboardType = UIKeyboardTypeNumberPad;
    _passLa.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_passLa.rac_textSignal subscribeNext:^(NSString *str) {
        
        if (str.length >=6) {
            self->_passLa.text = [str substringToIndex:6];
        }else
        {
            self->_passLa.text = str;
        }
    }];
    
    [self getUserInfo];

}

- (void)getUserInfo{
    
    MineStore *store = [[MineStore alloc] init];
    
    MJWeakSelf
    [store getUserInfoWithCustomId:[UserDefaultsManager sharedInstance].user_id Success:^(UserModel *model) {
        
        self->_phoneLa.text = [NSString stringWithFormat:@"%@*****%@",[model.phoneNumber substringToIndex:3],[model.phoneNumber substringFromIndex:8]];
        
    } Failure:^(NSError *error) {
        
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

#pragma mark - 获取验证码
- (IBAction)onCodeBtn:(UIButton *)sender {
    
    MJWeakSelf
    PutForwardRecordStore *store = [[PutForwardRecordStore alloc] init];

    [store getCodeWithPhone:nil Success:^{
        
        __block int n = 60;
        dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
        dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0);
        dispatch_source_set_event_handler(timer, ^{
            
            --n;
            NSLog(@"%i", n);
            [sender setTitle:[NSString stringWithFormat:@"%ds",n] forState:UIControlStateNormal];
            [sender setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            sender.backgroundColor = BLUECOLOR;
            if (0 == n) {
                dispatch_source_cancel(timer);
            }
        });
        // 计时器cancel的时候触发的block
        dispatch_source_set_cancel_handler(timer, ^{
            
            NSLog(@"cancel");
            [sender setTitle:@"重新获取" forState:UIControlStateNormal];
            [sender setTitleColor:BLUECOLOR forState:UIControlStateNormal];
            sender.backgroundColor = UIColor.whiteColor;
            
        });
        dispatch_resume(timer);
        
    } Failure:^(NSError *error) {
        
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}

#pragma mark - 密文状态控制
- (IBAction)onEyeBtn:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        
        _passLa.secureTextEntry = NO;
    }else{
        
        _passLa.secureTextEntry = YES;
    }
}

#pragma mark - 确定
- (IBAction)onSureBtn:(UIButton *)sender {
    
    if (_codeLa.text.length <= 0) {
        
        return [self showMBPError:@"请输入验证码"];
    }
    
    if (_passLa.text.length <= 0) {
        
        return [self showMBPError:@"请输入支付密码"];
    }
    
    MJWeakSelf
    SetStore *store = [[SetStore alloc] init];
    [store setTradingPassWordWithSecret:_passLa.text andValidateCode:_codeLa.text Success:^{
        
        [NewWalletList sharedInstance].tradingPassWordSeted = YES;
        
        SetTradingPassWordSuccessViewController *VC = [[SetTradingPassWordSuccessViewController alloc] init];
        VC.wherefrom = self.wherefrom;
        [weakSelf.navigationController pushViewController:VC animated:YES];
        
    } Failure:^(NSError *error) {
       
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

@end
